from unittest import TestCase
from ddt import ddt, data, unpack
from time import sleep
from selenium.webdriver.common.by import By

from TEST.Data.BuyData import BuyData
from TEST.Head.数据准备 import readyMysql
from TEST.FuncLogic.PageLogin import Login
from TEST.FuncLogic.PageBuy import Buy
from TEST.FuncLogic.Page import Page
from TEST.FuncLogic.Page import Qscreen


@ddt
class TestBuy(TestCase):
    readyMysql().delete()
    readyMysql().create()
    xpath = [['//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a',  # 零钱理财
              '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[6]/button'],
             ['//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[2]/a',  # 期限理财
              '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[8]/button'],
             ['//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[3]/a',  # 基金理财
              '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[1]/td[9]/button']]

    def 理财(self, username, password, payword, expect1, expect2, x):
        # driver信息
        driver = Page().driver
        # 登陆
        Login(driver).login(username, password)
        # 从数据库获取存款信息
        money = readyMysql().check_money(username)
        # 进行购买操作
        Buy(driver).buy(payword, self.xpath[x][0], self.xpath[x][1])
        sleep(1)

        result1 = driver.switch_to.active_element.text
        result1 = result1[:result1.find('\n')]

        sleep(0.5)

        driver.switch_to.active_element.click()

        now_money = readyMysql().check_money(username)

        # 获取结果信息
        result2 = str(int(money - now_money))

        # 判断
        Qscreen(driver, expect1, result1)
        Qscreen(driver, expect2, result2)

        # 关闭页面
        driver.quit()

        self.assertEqual(expect1, result1)
        self.assertEqual(expect2, result2)

    @data(*BuyData().data_零钱理财())
    @unpack
    def test_零钱理财(self, username, password, payword, expect1, expect2):
       self.理财(username, password, payword, expect1, expect2, 0)

    @data(*BuyData().data_期限理财())
    @unpack
    def test_期限理财(self, username, password, payword, expect1, expect2):
        self.理财(username, password, payword, expect1, expect2, 1)

    @data(*BuyData().data_基金理财())
    @unpack
    def test_基金理财(self, username, password, payword, expect1, expect2):
        self.理财(username, password, payword, expect1, expect2, 2)

    @data(*BuyData().data_银行卡支付()[:2]+BuyData().data_银行卡支付()[3:])
    @unpack
    def test_银行卡支付_1(self, username, password, payword, expect1, expect2):
        self.理财(username, password, payword, expect1, expect2, 2)

    @data(*BuyData().data_银行卡支付()[2:3])
    @unpack
    def test_银行卡支付_2(self, username, password, payword, expect1, expect2):
        # driver信息
        driver = Page().driver
        # 登陆
        Login(driver).login(username, password)
        # 点击个人中心
        driver.find_element(By.XPATH, "/ html / body / div / div / aside / div[2] / nav / ul / li[4] / a").click()
        # 点击银行卡管理
        driver.find_element(By.XPATH, "/html/body/div/div/aside/div[2]/nav/ul/li[4]/ul/li[3]/a").click()
        # 点击第二张银行卡的设为默认
        driver.find_element(By.XPATH, "/html/body/div/div/main/div/div/div[3]/div/div/div[1]/ul/li/button[3]").click()
        # 从数据库获取存款信息
        money = readyMysql().check_money(username)
        # 进行购买操作
        Buy(driver).buy(payword, self.xpath[2][0], self.xpath[2][1])
        sleep(0.5)

        result1 = driver.switch_to.active_element.text
        result1 = result1[:result1.find('\n')]

        driver.switch_to.active_element.click()

        now_money = readyMysql().check_money(username)

        # 获取结果信息
        result2 = str(int(money - now_money))

        # 判断
        Qscreen(driver, expect1, result1)
        Qscreen(driver, expect2, result2)

        # 关闭页面
        driver.quit()

        self.assertEqual(expect1, result1)
        self.assertEqual(expect2, result2)